﻿using System;
using System.Text;
using System.Threading;
using MbUnit.Framework;
using Selenium;
using ThoughtWorks.Selenium.Silvernium;

namespace SeleniumTest
{
    [TestFixture]
    public class SeleniumTestCase
    {
        private ISelenium selenium;
        private StringBuilder verificationErrors;

        [SetUp]
        public void SetupTest()
        {
            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost/");
            selenium.Start();
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                selenium.Stop();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }

        [Test]
        public void TheSeleniumTestCaseTest()
        {
            selenium.Open("/helloworld/default.aspx");
            selenium.Click("eat");
            selenium.Click("submitform");
            selenium.WaitForPageToLoad("30000");
           Assert.AreEqual("You ate a giant meatball", selenium.GetText("css=div.result"));
            selenium.Click("sleep");
            selenium.Click("submitform");
            selenium.WaitForPageToLoad("30000");
            Assert.AreEqual("You took a nap on the couch all day", selenium.GetText("css=div.result"));
        }









        [Test]
        public void SeleniumTestDemo5()
        {
            selenium.Open("/helloworld/demo5.aspx");

            // wait for the silverlight control to load
            Thread.Sleep(2000);
            var silver = new Silvernium(selenium, "silverlight");
            silver.SetPropertyValue("Sample.EatControl.Checked", "true");
            silver.ContentMethod("Sample.ClickDoitControl");

            // wait for the asynchronous call to complete
            while (silver.GetPropertyValue("Sample.IsProcessing") == "true")
                Thread.Sleep(500);
            Assert.AreEqual("You ate a giant meatball", silver.GetPropertyValue("Sample.Result"));
        }
    }
}
